package com.controller.merchant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.dto.merchant.Level1DTO;
import com.dto.merchant.Level2DTO;
import com.dto.merchant.Level3DTO;
import com.model.dao.Level1Table;
import com.model.dao.Level1TableDAO;
import com.model.dao.Level2Table;
import com.model.dao.Level2TableDAO;
import com.model.dao.Level3Table;
import com.model.dao.Level3TableDAO;

@ManagedBean(name = "organizeMenuController")
@SessionScoped
public class OrganizeMenuController implements Serializable {

	private static final long serialVersionUID = -1385469162004245938L;

	public List<Level1DTO> findAll() throws Exception {
		Level1TableDAO dao = new Level1TableDAO();
		List<Level1Table> list = dao.findAll();
		List<Level1DTO> level1dtos = new ArrayList<Level1DTO>();
		for (Level1Table var : list) {
			Level1DTO level1dto = new Level1DTO();
			level1dto.setL1Id(var.getL1Id());
			level1dto.setL1Name(var.getL1Name());
			List<Level2DTO> level2dtos = new ArrayList<Level2DTO>();
			for (Level2Table level2Table : var.getLevel2Tables()) {
				Level2DTO level2dto = new Level2DTO();
				level2dto.setL2Id(level2Table.getL2Id());
				level2dto.setL2Name(level2Table.getL2Name());
				List<Level3DTO> level3dtos = new ArrayList<Level3DTO>();
				for (Level3Table level3Table : level2Table.getLevel3Tables()) {
					Level3DTO level3dto = new Level3DTO();
					level3dto.setL3Id(level3Table.getL3Id());
					level3dto.setL3Name(level3Table.getL3Name());
					level3dtos.add(level3dto);
				}
				Collections.sort(level3dtos);
				level2dto.setLevel3Tables(level3dtos);
				level2dtos.add(level2dto);
			}
			Collections.sort(level2dtos);
			level1dto.setLevel2Tables(level2dtos);
			level1dtos.add(level1dto);
		}
		Collections.sort(level1dtos);
		return level1dtos;
	}

	public void createL1Item(Level1DTO level1dto){
		Level1TableDAO dao=new Level1TableDAO();
		Level1Table table=new Level1Table();
		table.setL1Name(level1dto.getL1Name());
		dao.persist(table);
	}
	
	public void updateL1Item(Level1DTO level1dto){
		Level1TableDAO dao=new Level1TableDAO();
		Level1Table table =dao.findById(level1dto.getL1Id());
		table.setL1Name(level1dto.getL1Name());
		dao.merge(table);
	}
	
	public void createL2Item(Level2DTO level2dto){
		Level2TableDAO dao=new Level2TableDAO();
		Level2Table table=new Level2Table();
		table.setL2Name(level2dto.getL2Name());
		Level1Table level1Table=new Level1Table();
		level1Table.setL1Id(level2dto.getLevel1dto().getL1Id());
		table.setLevel1Table(level1Table);
		dao.persist(table);
	}
	
	public void updateL2Item(Level2DTO level2dto){
		Level2TableDAO dao=new Level2TableDAO();
		Level2Table table =dao.findById(level2dto.getL2Id());
		table.setL2Name(level2dto.getL2Name());
		dao.merge(table);
	}
	
	public void createL3Item(Level3DTO level3dto){
		Level3TableDAO dao=new Level3TableDAO();
		Level3Table table=new Level3Table();
		table.setL3Name(level3dto.getL3Name());
		Level2Table level2Table=new Level2Table();
		level2Table.setL2Id(level3dto.getLevel2dto().getL2Id());
		table.setLevel2Table(level2Table);
		dao.persist(table);
	}
	
	public void updateL3Item(Level3DTO level3dto){
		Level3TableDAO dao=new Level3TableDAO();
		Level3Table table =dao.findById(level3dto.getL3Id());
		table.setL3Name(level3dto.getL3Name());
		dao.merge(table);
	}
	public static void main(String[] args) throws Exception {
		OrganizeMenuController controller = new OrganizeMenuController();
		controller.findAll();
	}
}
